package cn.nabaixin.latest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import cn.nabaixin.R;
import cn.nabaixin.mine.MineActivity;
import cn.nabaixin.setting.SettingActivity;
import cn.nabaixin.sign.SignDetailActivity;
import cn.nabaixin.utils.AppUtils;
/**
 * 最新动态Activity
 * @author ZWH
 *
 */
public class LatestActivity extends ListActivity {
	
	private ListView lv;
	private SimpleAdapter sa;
	private List<Map<String, Object>> data;
	private TextView refreshView;
	private TextView refreshTimeView;
	private View headerView;
	private TextView footView;
	private Context c;
	//长按弹出菜单
	public final int MENU_USER_PAGE = 1;
	public final int MENU_HOME_PAGE = 2;
	public final int MENU_MSG_DETAIL = 3;
	//菜单项
	public final int MENU_REFRESH = 4;
	public final int MENU_TOTOP = 5;
	public final int MENU_SAYSTH = 6;
	public final int MENU_SETTING = 7;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		c = LatestActivity.this;
		initViews();
		initListener();
		initData();
		
		
	}
	/**
	 * 初始化界面
	 */
	private void initViews(){
		lv = getListView();
		lv.setBackgroundColor(Color.WHITE);
		lv.setCacheColorHint(0);
		headerView = getLayoutInflater().inflate(R.layout.latest_list_header, null);
		footView = new TextView(c);
		footView.setText("加载更多...");
		footView.setId(10);
		footView.setHeight(80);
		footView.setGravity(Gravity.CENTER);
		lv.addHeaderView(headerView);
		lv.addFooterView(footView);
		registerForContextMenu(lv);	//注册ContextMenu
	}
	private void initListener(){
		View.OnClickListener clickListener = new ClickListener();
		refreshView = (TextView) headerView.findViewById(R.id.latest_list_refresh);
		refreshTimeView = (TextView) headerView.findViewById(R.id.latest_list_refershTime);
		refreshView.setOnClickListener(clickListener);
		refreshTimeView.setOnClickListener(clickListener);
		footView.setOnClickListener(clickListener);
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				goDetailActivity();
			}
			
		});
	}
	/**
	 * 初始化数据
	 */
	private void initData(){
		//初始化静态数据
		String names[] = new String[]{"张三", "李四", "王五"};
		String positions[] = new String[]{"北京西直门", "上地城铁", "时代之光1号楼"};
		String contents[] = new String[]{"状态好才是真的好!状态好才是真的好！", "好好学习天天向上！哈哈哈哈哈哈哈哈！！！！！", "招行行长:房价上涨是因为百姓手中钱太多？？？"};
		String sendTiems[] = new String[]{"一周前", "两天前", "一天前", "上午", };
		String cmts[] = new String[]{"10", "20", "5", "33", };
		Integer photos[] = new Integer[]{null, R.drawable.temp_photo};
		
		data = new ArrayList<Map<String,Object>>();
		for(int i=0; i<7; i++){
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("name", AppUtils.getRandomItem(names));
			item.put("position", AppUtils.getRandomItem(positions));
			item.put("content", AppUtils.getRandomItem(contents));
			item.put("sendtime", AppUtils.getRandomItem(sendTiems));
			item.put("cmts", AppUtils.getRandomItem(cmts));
			int imgId = R.drawable.temp_photo01;
			if(i==2)
				imgId = R.drawable.temp_photo;
			item.put("photo", imgId);
			
			data.add(item);
		}
		
		sa = new SimpleAdapter(c, data, R.layout.latest_list_item, 
				new String[]{"name", "position", "content", "sendtime", "cmts", "photo"}, 
				new int[]{R.id.user_name, R.id.user_position, R.id.user_content, R.id.user_sendTime, R.id.user_cmts, R.id.user_photo});
		lv.setAdapter(sa);
	}
	

	//长按弹出菜单
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		menu.setHeaderTitle("操作");
		menu.add(0, MENU_USER_PAGE, 0, "去TA的主页");
		menu.add(0, MENU_HOME_PAGE, 1, "进入官网");
		menu.add(0, MENU_MSG_DETAIL, 2, "查看详情");
		super.onCreateContextMenu(menu, v, menuInfo);
	}
	//处理长按菜单项
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		switch (itemId) {
		case MENU_USER_PAGE:	//进入用户主页
			Intent intent = new Intent(c, MineActivity.class);
			startActivity(intent);
			return true;
		case MENU_HOME_PAGE:	//进入官网主页
			Toast.makeText(c, "MENU_HOME_PAGE", 0).show();
			return true;
		case MENU_MSG_DETAIL:	//查看详情
			goDetailActivity();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
	//添加菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_REFRESH, 0, "刷新").setIcon(R.drawable.menu_refresh);
		menu.add(0, MENU_SAYSTH, 1, "喊话").setIcon(R.drawable.menu_saysth);
		menu.add(0, MENU_TOTOP, 2, "回顶部").setIcon(R.drawable.menu_gotop);
		menu.add(0, MENU_SETTING, 3, "设置").setIcon(R.drawable.menu_setting);
		return super.onCreateOptionsMenu(menu);
	}
	//处理菜单点击
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		switch (itemId) {
		case MENU_REFRESH:
			Toast.makeText(c, "MENU_REFRESH", 0).show();
			return true;
		case MENU_SAYSTH:	//喊话
			Intent intent1 = new Intent(c, SaySth.class);
			startActivity(intent1);
			return true;
		case MENU_TOTOP:
			Toast.makeText(c, "MENU_TOTOP", 0).show();
			return true;
		case MENU_SETTING:
			Intent intent2 = new Intent(c, SettingActivity.class);
			startActivity(intent2);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
		
	}
	//进入签名详情页面
	private void goDetailActivity(){
		Intent intent = new Intent(c, SignDetailActivity.class);
		startActivity(intent);
	}
	private class ClickListener implements View.OnClickListener{
		@Override
		public void onClick(View v) {
			int viewId = v.getId();
			switch (viewId) {
			case R.id.latest_list_refresh:
			case R.id.latest_list_refershTime:	//点击刷新
				Toast.makeText(c, "点击了刷新", 0).show();
				break;
			case 10:	//点击刷新
				Toast.makeText(c, "点击了获取更多", 0).show();
				break;

			}
		}
		
	}
}
